using System;
using CnSharp.Windows.Updater.Util;
using EnvDTE;
using EnvDTE80;
using Microsoft.VisualStudio.CommandBars;

namespace CnSharp.Delivery.VisualStudio.PackingTool.Commands
{
    public abstract class CommandMenuItem
    {
        // Fields
        protected readonly Logger Logger = Common.MyLogger;

        // Methods
        protected CommandMenuItem()
        {
        }

        public virtual CommandBarControl Connect()
        {
            Commands2 commands = (Commands2) this.Dte.Commands;
            Microsoft.VisualStudio.CommandBars.CommandBars commandBars = (Microsoft.VisualStudio.CommandBars.CommandBars) this.Dte.DTE.CommandBars;
            Microsoft.VisualStudio.CommandBars.CommandBar owner = commandBars[this.ProjectNodeName];
            try
            {
                object[] contextUIGUIDs = new object[0];
                Command command = null;
                try
                {
                    command = commands.Item(this.AddIn.ProgID + "." + this.CommandName, -1);
                    if (command != null)
                    {
                        command.Delete();
                    }
                }
                catch
                {
                }
                command = commands.AddNamedCommand2(this.AddIn, this.CommandName, this.MenuText, this.MenuText, true, this.FaceId, ref contextUIGUIDs, 3, 3, vsCommandControlType.vsCommandControlTypeButton);
                if (command != null)
                {
                    if (!string.IsNullOrEmpty(this.HotKey))
                    {
                        command.Bindings = string.Format("Global::{0}", this.HotKey);
                    }
                    return (CommandBarControl) command.AddControl(owner, owner.Controls.Count + 1);
                }
                return null;
            }
            catch (ArgumentException exception)
            {
                this.Logger.WriteDebugLog(exception.Message + exception.StackTrace);
                return null;
            }
        }

        public virtual bool Execute()
        {
            return false;
        }

        // Properties
        public AddIn AddIn { get; set; }

        public string CommandName { get; set; }

        public DTE2 Dte { get; set; }

        public int FaceId { get; set; }

        public string HotKey { get; set; }

        public string MenuText { get; set; }

        public string ProjectNodeName { get; set; }
    }
}